﻿using Microsoft.EntityFrameworkCore;
using shenghua.Authorization.Roles;
using shenghua.Basics.Areas;
using shenghua.Basics.MemberFormItems;
using shenghua.Basics.MemberLevels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.EntityFrameworkCore.Seed.Host
{
    public class DefaultDataCreator
    {
        private readonly shenghuaDbContext _context;

        public DefaultDataCreator(shenghuaDbContext context)
        {
            _context = context;
        }

        public void Create()
        {
            CreateDefaultArea();
            CreateDefaultMemberLevel();
            CreateDefaultMemberFormItem();
        }

        public void CreateDefaultArea()
        {
            void AddIfNotExists(Area area)
            {
                if (_context.Areas.IgnoreQueryFilters().Any(r => r.Name == area.Name))
                {
                    return;
                }
                _context.Areas.Add(area);
                _context.SaveChanges();
            }

            AddIfNotExists(new Area { Name = "华东", DisplayName = "华东", Enabled = true, IsStatic = true });
            AddIfNotExists(new Area { Name = "华北", DisplayName = "华北", Enabled = true, IsStatic = true });
            AddIfNotExists(new Area { Name = "华中", DisplayName = "华中", Enabled = true, IsStatic = true });
            AddIfNotExists(new Area { Name = "华南", DisplayName = "华南", Enabled = true, IsStatic = true });
            AddIfNotExists(new Area { Name = "东北", DisplayName = "东北", Enabled = true, IsStatic = true });
            AddIfNotExists(new Area { Name = "西北", DisplayName = "西北", Enabled = true, IsStatic = true });
            AddIfNotExists(new Area { Name = "西南", DisplayName = "西南", Enabled = true, IsStatic = true });
            AddIfNotExists(new Area { Name = "港澳台", DisplayName = "港澳台", Enabled = true, IsStatic = true });

        }

        public void CreateDefaultMemberLevel()
        {
            void AddIfNotExists(MemberLevel memberLevel)
            {
                if (_context.MemberLevels.IgnoreQueryFilters().Any(r => r.Name == memberLevel.Name))
                {
                    return;
                }
                _context.MemberLevels.Add(memberLevel);
                _context.SaveChanges();
            }

            AddIfNotExists(new MemberLevel
            {
                Name = "普通会员",
                Discount = 1,
                DisplayName = "普通会员",
                NeededXPValue = 0,
                Roles = new Common.CustomValueType.ValueArray<string>(new List<string> { "Member" }),
                IsStatic = true
            });

        }

        public void CreateDefaultMemberFormItem()
        {
            void AddIfNotExists(MemberFormItem item)
            {
                if (_context.MemberFormItems.IgnoreQueryFilters().Any(r => r.Name == item.Name))
                {
                    return;
                }
                _context.MemberFormItems.Add(item);
                _context.SaveChanges();
            }

            AddIfNotExists(new MemberFormItem
            {
                Name = "phoneNumber",
                DisplayName = "手机",
                Type = MemberFormItemType.Input,
                Enabled = true,
                IsRequired = true,
                EnableWhenRegister = true,
                IsStatic = true
            });
            AddIfNotExists(new MemberFormItem
            {
                Name = "EmailAddress",
                DisplayName = "邮箱",
                Type = MemberFormItemType.Input,
                Enabled = true,
                IsRequired = true,
                EnableWhenRegister = true,
                IsStatic = true
            });

        }

        //public void CreateDefaultRole()
        //{
        //    void AddIfNotExists(Role area)
        //    {
        //        if (_context.Areas.IgnoreQueryFilters().Any(r => r.Name == area.Name))
        //        {
        //            return;
        //        }
        //        _context.Roles.Add(area);
        //        _context.SaveChanges();
        //    }

        //    AddIfNotExists(new Role { Name = "Member", DisplayName = "Member", Permissions = new List<String> { "Member" }, IsStatic = true });

        //}
    }
}
